package xml;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author 孙许
 * @date 2018-04-18
 * @description
 */
public class StaxDemo {

    public void parse(File file) throws FileNotFoundException, XMLStreamException {
        XMLInputFactory factory = XMLInputFactory.newFactory();
        XMLStreamReader parser = factory.createXMLStreamReader(new FileInputStream(file));
        while (parser.hasNext()) {
            if (parser.next() == XMLStreamConstants.START_ELEMENT) {
                if ("name".equals(parser.getLocalName())) {
                    System.out.println(parser.getElementText());
                }
            }
        }
    }
}
